package com.atstudy.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/10 13:51
 * @copyright: 博为峰
 */
public class JdbcDemo01 {
    public static void main(String[] args) {
//        m1();
//        m2();
//        m3();
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/29db", "root", "123456");
            //预编译
            PreparedStatement preparedStatement = connection.prepareStatement("insert into stu(name,sex,cla_name,age,score,cla_id) values (?,?,?,?,?,?)");
//            while (true) {
                preparedStatement.setString(1, "兔年");
                preparedStatement.setString(2, "年");
                preparedStatement.setString(3, "29期");
                preparedStatement.setInt(4, 2023);
                preparedStatement.setInt(5, 2023);
                preparedStatement.setInt(6, 1);
                int i = preparedStatement.executeUpdate();
//            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    private static void m3() {
        ArrayList<Student> list = new ArrayList<Student>();
        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/29db", "root", "123456");
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("select * from stu");) {
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setSex(resultSet.getString("sex"));
                student.setClaName(resultSet.getString("cla_name"));
                student.setAge(resultSet.getInt("age"));
                student.setScore(resultSet.getInt("score"));
                student.setCreateTime(resultSet.getDate("create_time"));
                student.setUpdateTime(resultSet.getDate("update_time"));
                student.setClaId(resultSet.getInt("cla_id"));
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Stream<Student> stream = list.stream();
        stream.forEach(System.out::println);
    }

    private static void m2() throws SQLException {
        ArrayList<Student> list = new ArrayList<Student>();
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/29db", "root", "123456");
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from stu");
        while (resultSet.next()) {
            Student student = new Student();
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setSex(resultSet.getString("sex"));
            student.setClaName(resultSet.getString("cla_name"));
            student.setAge(resultSet.getInt("age"));
            student.setScore(resultSet.getInt("score"));
            student.setCreateTime(resultSet.getDate("create_time"));
            student.setUpdateTime(resultSet.getDate("update_time"));
            student.setClaId(resultSet.getInt("cla_id"));
            list.add(student);
        }
        Stream<Student> stream = list.stream();
//        stream.forEach(new Consumer<Student>() {
//            @Override
//            public void accept(Student student) {
//                System.out.println("student = " + student);
//            }
//        });

//        stream.forEach(student -> System.out.println("student = " + student));

        stream.forEach(System.out::println);
    }

    private static void m1() throws SQLException {
        //加载驱动
//        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/29db", "root", "123456");
        //执行SQL语句
        Statement statement = connection.createStatement();
        //结果集
        ResultSet resultSet = statement.executeQuery("select * from stu");
        while (resultSet.next()) {
            String name = resultSet.getString("name");
//            int id = resultSet.getInt("id");
            int id = resultSet.getInt(1);
            System.out.println("id = " + id);
            System.out.println("name = " + name);

            Date createTime = resultSet.getDate("create_time");
        }
        resultSet.close();
        statement.close();
        connection.close();
//        System.out.println(connection);
    }
}
